// pages/my/coupon.js
import Router from '../../utils/router.js';
import base from '../../config/base.js';
import util from '../../utils/util.js'
import myindexService from '../../services/myindex.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    not_use: 0,
    have_use: 0,
    have_invilad: 0,
    nocoupon: base.static + '/my/nocoupon.png',
    coupon_bk: base.static + '/my/coupon_bk2.png',
    coupon_ky: base.static + '/my/coupon_ky2.png',
    shixiao: base.static + '/my/shixiao.png',
    yshiyong: base.static + '/my/yshiyong.png',
    wshiyong: base.static + '/my/wshiyong.png',
    couponlength: 1,
    status: '',
    loadflag: 1,
    offset: 1,
    limit: 20,
    not_use_list: [],
    have_use_list: [],
    have_invilad_list: [],
    listflag: false,
  },
  qh_coupon(e){
    let status = e.currentTarget.dataset.status;
    if (status != this.data.status) {
      this.setData({
        status,
        couponlength: 1,
        loadflag: 1,
        offset: 1
      });
      this.getcouponlist();
    }
  },
  getcouponlist(){
    let status = this.data.status || 'not_use',
       loadflag = '';
    if(this.data.offset ==1){
      this.setData({
        not_use_list: [],
        have_use_list: [],
        have_invilad_list: [],
      });
    }
    myindexService.getinfocouponlist({
      type: status,
      offset: this.data.offset,
      limit: this.data.limit,
      }, res=>{
      if(res && res.code==1){
        let list = res.data.data,
          current_time = res.data.current_time;
        for(let i in list){
          let item = list[i];
          item.type = status;
          item.scene = 'my';
          try{
            item.full = util.floatAccMul(item.full, 1);
            item.reduction = util.floatAccMul(item.reduction, 1);
          } catch (e) { }
          let end = util.dateAmu(current_time) - util.dateAmu(item.period_validity_e);
          if (end <= 0 && item.is_use == 0) {
            item.status = 1;
          } else if (end > 0 || item.is_use == 1) {
            item.status = 0;
          }
        }
        list = this.data[status +'_list'].concat(list);
        let offset = this.data.offset;
        if (res.data.data.length < this.data.limit) {
          loadflag = 0;
        } else {
          offset++;
          loadflag = 1;
        }
        this.setData({
          [status + '_list']: list,
          current_time,
          loadflag,
          offset
        });
      } else {
        loadflag = 0;
        let couponlength = this.data[status + '_list'].length;
        this.setData({
          loadflag, couponlength
        });
      }
    }, res => {
      this.setData({
        listflag: true
      });
      loadflag = -1;
      wx.stopPullDownRefresh();
      if (res && res.statusCode != 200) {
        this.setData({
         loadflag
        });
      }
    });
    console.log("have_use_list", this.data.have_use_list)
  },
  //去兑换劵码页面
  goexchange(){
    if (this.data.status == 'have_use' ||'have_invilad'){
      this.setData({
        status: 'not_use',
        couponlength: 1
      })
    }
    if (this.data.status == 'not_use') {
      this.setData({
        couponlength: 1
      })
    }
    Router.go('exchange',`list=2`) 
  },
  /**
   * 获取优惠券数量
   */
  getcouponnum() {
    myindexService.getcouponnum({}, res => {
      if (res && res.code == 1) {
        this.setData({
          not_use: res.data.not_use,
          have_use: res.data.have_use,
          have_invilad: res.data.have_invilad,
        });
      }
    }, res=>{
      this.getcouponlist();
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      offset: 1
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getcouponnum();    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      offset: 1,
      loadflag: 1
    });
    this.getcouponnum();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this,
      loadflag = that.data.loadflag,
      listflag = that.data.listflag;
    if (!listflag) {
      return;
    }
    that.setData({
      listflag: false
    });
    if (loadflag == 1) {
      let offset = that.data.offset;
      this.getcouponnum();
    }
  }
})